//
//  HWBookDetailView.m
//  嗨文
//
//  Created by 响当当 on 2019/3/11.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWBookDetailView.h"
#import "HWBookDetailCell.h"
#import "HWBookIntroductionCell.h"
#import "HWBookIndexCell.h"

@interface HWBookDetailView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, copy)NSArray *bookDeatailArr;
@property (nonatomic, assign)NSInteger tagNum;
@end

@implementation HWBookDetailView

- (NSArray *)bookDeatailArr
{
    if (!_bookDeatailArr)
    {
        _bookDeatailArr = @[@{@"bookCommon":@"bookCommon",@"title":@"英雄联盟之最强异世 升级系统",@"author":@"萧瑾瑜",@"subTitle":@" 707万字  |  已完结",@"tag":@[@"东方玄幻",@"仙侠",@"仙侠"],@"click":@"276517",@"pursure":@"276517",@"share":@"276517"},
                            @{@"introduce":@"他，落魄家族子弟，饱受他人白眼，被骂废柴。少年不甘，敢逆天命，历经千难万险，一步步开启了他波澜壮阔的武道人生。热血武道，有恩报恩，有仇报仇。练武升级，大杀四方。杀怪征途，收服异兽。进入秘境，激化神脉。踏入神道，且看他如何成就自己的武道传奇！"},
                            @{@"index":@"最新章节：第130章 游戏死了你得活啊！",@"time":@"更新于12小时前"}];
    }
    return _bookDeatailArr;
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;
        _tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    return _tableView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self addSubview:self.tableView];
    }
    return self;
}

#pragma mark - tableView delegate datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return 210*kUIScaleBasePlus;
    }
    else if(indexPath.section == 1)
    {
        if (self.tagNum == 0)
        {
            return 86*kUIScaleBasePlus;
        }
        else
        {
            HWBookIntroductionCell *cell = [[HWBookIntroductionCell alloc]init];
            
            return [cell getBackHeight:self.bookDeatailArr[indexPath.section][@"introduce"]];
        }
    }
    else if(indexPath.section == 2)
    {
        return 94*kUIScaleBasePlus;
    }
    else
    {
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10*kUIScaleBasePlus;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *backCell = nil;
    
    if (indexPath.section == 0)
    {
        HWBookDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BookDetail"];
        if (!cell)
        {
            cell = [[HWBookDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BookDetail"];
            cell.selectionStyle = 0;
        }
        
        [cell getDate:self.bookDeatailArr[indexPath.section]];
        
        backCell = cell;
        
    }
    else if(indexPath.section == 1)
    {
        HWBookIntroductionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"IntroductionCell"];
        if (!cell)
        {
            cell = [[HWBookIntroductionCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"IntroductionCell"];
            cell.selectionStyle = 0;
        }
        
        [cell getIntroduce:self.bookDeatailArr[indexPath.section] andNum:self.tagNum];

        WeakSelf(self)
        cell.openIntroduceBlock = ^(NSInteger tag)
        {
            weakSelf.tagNum = tag;
           [weakSelf.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
        };
        
        
        backCell = cell;
    }
    else if(indexPath.section == 2)
    {
        HWBookIndexCell *cell = [tableView dequeueReusableCellWithIdentifier:@"IndexCell"];
        if (!cell)
        {
            cell = [[HWBookIndexCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"IndexCell"];
            cell.selectionStyle = 0;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        
        [cell getDic:self.bookDeatailArr[indexPath.section]];
        
        backCell = cell;
    }
    else
    {
        backCell = [UITableViewCell new];
    }
    
    
    
    
    return backCell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section)
    {
        case 0:
        {
            
        }
            break;
        case 1:
        {
            
        }
            break;
        default:
            break;
    }
}

@end
